/**
 * 核心表格合并模块
 * @description 有子集设置colspan、没有子集设置rowspan
 */
(function (G, Utils) {
  const { getColumnCount } = Utils;

  /**
   * 格式化表土数据
   * @param {array} headerTree 
   * @returns {array}
   */
  function parseHeaderData(dataTee) {
    const _d = JSON.parse(JSON.stringify(dataTee));
    let resultGroup = {};
    const _walk = (data, __level = 1) => {
      const groupKey = `level_${__level}`;
      for (let i = 0, len = data.length; i < len; i++) {
        const column = data[i];
        // 是否存在子集
        if (column.children && column.children.length) {
          column.rowspan = 1;
          column.colspan = getColumnCount(column);
          _walk(column.children, __level + 1); // 继续往下走
        } else {
          column.rowspan = data.length || 1;
          column.colspan = 1;
        }
        if (!resultGroup[groupKey]) {
          resultGroup[groupKey] = []
        }
        resultGroup[groupKey].push(column)
      }
      return resultGroup;
    }
    return Object.keys(_walk(_d))
      .sort()
      .map(key => resultGroup[key]);
  }
  
  // export
  G.parseHeaderData = G.parseHeaderData || parseHeaderData
})(window, window.utils);